<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件下载器</title>
</head>
<body>
  <script type="text/javascript">
    const readableStream = new ReadableStream({
    start(controller) {
      controller.enqueue('h')
      controller.enqueue('e')
      controller.enqueue('l')
      setTimeout(() => {
        controller.enqueue('l')
        controller.enqueue('o')
        controller.close()
      }, 1000)
    },
    // 每次获取数据块时调用，直到内部的缓冲队列被填满
    pull(controller) {
      console.log('readable pull--->')
    },
    // 终止发送流时触发
    cancel(reason) {
      console.log('readable cancel--->', reason)
    },
  })

  const readLoad = async () => {
    const reader = readableStream.getReader()
    let str = ''
    while (true) {
      const { value, done } = await reader.read()
      if (done) {
        return str
      }
      str += value
      console.log('value--->',  value)
    }
  }
  readLoad()
  </script>
</body>
</html>